package com.ruoyi.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.domain.TMessageInfo;
import com.ruoyi.business.enums.MessageReadStatusEnums;
import com.ruoyi.business.service.ITMessageInfoService;
import com.ruoyi.business.vo.TMessageInfoVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 消息通知Controller
 *
 * @author Tellsea
 * @date 2024-01-26
 */
@Api(tags = "消息通知Controller")
@RestController
@RequestMapping("/business/messageInfo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TMessageInfoController extends BaseController {

    private final ITMessageInfoService tMessageInfoService;

    @ApiOperation("查询消息通知列表")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<TMessageInfoVo> list(TMessageInfoVo entity) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        entity.setReceiveUserId(userId);
        return tMessageInfoService.queryList(entity);
    }

    @ApiOperation("查询消息通知所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TMessageInfoVo entity) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        entity.setReceiveUserId(userId);
        return AjaxResult.success("查询成功", tMessageInfoService.queryAll(entity));
    }

    @ApiOperation("导出消息通知列表")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMessageInfoVo entity) {
        List<TMessageInfoVo> list = tMessageInfoService.queryAll(entity);
        ExcelUtil<TMessageInfoVo> util = new ExcelUtil<>(TMessageInfoVo.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    @ApiOperation("获取消息通知详细信息")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tMessageInfoService.queryById(id));
    }

    @ApiOperation("新增消息通知")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TMessageInfoVo entity) {
        return toAjax(tMessageInfoService.save(entity));
    }

    @ApiOperation("修改消息通知")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TMessageInfoVo entity) {
        return toAjax(tMessageInfoService.updateById(entity));
    }

    @ApiOperation("删除消息通知")
    @PreAuthorize("@ss.hasPermi('business:messageInfo:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMessageInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("当前用户未读消息数量")
    @GetMapping("/getCurrNoReadCount")
    public AjaxResult getCurrNoReadCount() {
        return AjaxResult.success("查询成功", tMessageInfoService.count(Wrappers.<TMessageInfo>lambdaQuery()
                .eq(TMessageInfo::getReceiveUserId, SecurityUtils.getLoginUser().getUserId())
                .eq(TMessageInfo::getReadStatus, MessageReadStatusEnums.UNREAD.getCode())));
    }
}
